import 'package:concierge/app/pages/splash/state.dart';
import 'package:concierge/app/routes/app_routes.dart';
import 'package:concierge/app/utils/Cacheutil.dart';
import 'package:concierge/generated/assets.dart';
import 'package:get/get.dart';
import 'package:video_player/video_player.dart';

class SplashLogic extends GetxController {
  final state = SplashState();
  @override
  onReady() {
    super.onReady();
    init();
  }

  Future<void> init() async {
    state.controller.value = VideoPlayerController.asset(Assets.videoWelcome);
    state.isInitialized.value = false;
    await state.controller.value!.initialize();
    state.isInitialized.value = true;
    state.controller.value!.setVolume(1.0);
    state.controller.value!.setPlaybackSpeed(4.0);
    state.controller.value!.play();
    state.controller.value!.addListener(() async {
      if (state.controller.value == null) {
        return;
      }
      if (!state.controller.value!.value.isPlaying &&
          state.controller.value!.value.position >=
              state.controller.value!.value.duration) {
        if (await getUser() != null) {
          Get.offAllNamed(Routes.login);
        } else {
          Get.offAllNamed(Routes.login);
        }
      }
    });
  }

  @override
  void onClose() {
    super.onClose();
    state.controller.value?.pause();
    state.controller.value?.dispose();
  }
}
